package com.example.user.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/sentinel")
@SentinelResource(value = "SentinelController")
public class SentinelController {

    @GetMapping("/test")
    @SentinelResource(blockHandler = "infoBlockHandler",fallback = "test_fallback")
    public String test(){
        System.out.println("api 8081/sentinel/test");
        if(true) throw new RuntimeException("原地爆炸");
        System.out.println("success respond");
        return "success respond";
    }

    /**
     * 定义降级方法：
     *  1. 方法的返回值需要和原方法一样
     *  2. 方法的参数需要和原方法一样
     */
    public String test_fallback(Throwable e){
        return "sentinel：provider端降级了-fallback-"+e.getMessage();
    }

    public String infoBlockHandler( BlockException e) {
        return  "sentinel：系统繁忙,稍后再试-blockHandler-"+e.getMessage();
    }
}
